package com.aplicacaoexemploweb.dao;

import com.aplicacaoexemploweb.exceptions.AlunoException;
import com.aplicacaoexemploweb.pojo.Aluno;
import com.aplicacaoexemploweb.util.HibernateUtil;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author Pc
 */
public class AlunoDaoImpl implements AlunoDao {

    @Override
    public Aluno add(Aluno aluno) throws AlunoException {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction trans = session.beginTransaction();
        try {
            session.save(aluno);
            session.refresh(aluno);
            trans.commit();
            return aluno;
        } catch (Exception e) {
            trans.rollback();
            throw new AlunoException(e.getMessage(), e.getCause());
        }
    }
    
    @Override
    public Aluno update(Aluno aluno) throws AlunoException {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction trans = session.beginTransaction();
        try {
            session.update(aluno);
            session.refresh(aluno);
            trans.commit();
            return aluno;
        } catch (Exception e) {
            trans.rollback();
            throw new AlunoException(e.getMessage(), e.getCause());
        }
    }
    
    @Override
    public Aluno remove(Aluno aluno) throws AlunoException {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction trans = session.beginTransaction();
        try {
            session.delete(aluno);
            session.refresh(aluno);
            trans.commit();
            return aluno;
        } catch (Exception e) {
            trans.rollback();
            throw new AlunoException(e.getMessage(), e.getCause());
        }
    }

    @Override
    public Aluno buscaPorId(Long id) throws AlunoException {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Aluno aluno = null;
        try {
            Criteria criteria = session.createCriteria(Aluno.class);
            criteria.add(Restrictions.like("id", id));
            aluno = (Aluno) criteria.uniqueResult();
            return aluno;
        } catch (Exception e) {
            throw new AlunoException(e.getMessage(), e.getCause());
        }
    }

    @Override
    public Aluno buscaPorEmail(String email) throws AlunoException {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Aluno aluno = null;
        try {
            Criteria criteria = session.createCriteria(Aluno.class);
            criteria.add(Restrictions.like("email", email));
            aluno = (Aluno) criteria.uniqueResult();
            return aluno;
        } catch (Exception e) {
            throw new AlunoException(e.getMessage(), e.getCause());
        }
    }
}
